import { Component } from "vue";
import {
  InputProps,
  FormItemProps,
  type InputNumberProps,
  type SelectProps,
  type TimePickerProps,
  type TimeRangePickerProps,
  type RadioGroupProps,
} from "ant-design-vue";
import { Param } from "@/components/autoForm/custom/loadselect/api.ts";
import { DefaultOptionType } from "ant-design-vue/es/vc-select/Select";
import { TAnyField } from "@/utils/types/key-value-any.ts";

interface IBaseOption<
  F,
  T extends TAnyField,
  R extends TAnyField,
  S extends keyof TAnyField = string,
> {
  label?: string;
  field: keyof F;
  props?: T;
  value?: T["value"]; // value属性的类型
  rules?: R["rules"];
  slot?: Partial<Record<S, string>>;
  hide?: boolean;
  span?: number;
}

interface ICustomOption<
  F,
  T extends TAnyField,
  R extends TAnyField,
  S extends keyof TAnyField = string,
> extends IBaseOption<F, T, R, S> {
  type: "custom";
  component?: Component;
}

export type TFormItemComponent =
  | "input"
  | "inputNumber"
  | "select"
  | "timePicker"
  | "timeRangePicker"
  | "custom"
  | "loadSelect"
  | "radio";
type TExtends<T> = T & { component?: Component };
type TInputSlotKey = "addonAfter" | "addonBefore" | "prefix" | "suffix";
type TSelectSlotKey =
  | "clearIcon"
  | "dropdownRender"
  | "maxTagPlaceholder"
  | "menuItemSelectedIcon"
  | "notFoundContent"
  | "option"
  | "placeholder"
  | "removeIcon"
  | "suffixIcon"
  | "tagRender";

interface IOption<
  K extends TFormItemComponent,
  F,
  T extends TAnyField,
  R extends TAnyField,
  S extends keyof TAnyField = string,
> extends IBaseOption<F, T, R, S> {
  type: K;
}

export interface LoadSelectProps extends SelectProps {
  url: string;
  param?: Param[];
  // @ts-ignore
  handle: (data) => DefaultOptionType;
  lazy?: boolean;
}

export type FormOption<F> =
  | IOption<"input", F, InputProps, FormItemProps, TInputSlotKey>
  | IOption<"inputNumber", F, InputNumberProps, FormItemProps, TInputSlotKey>
  | IOption<"select", F, SelectProps, FormItemProps, TSelectSlotKey>
  | IOption<"timePicker", F, TimePickerProps, FormItemProps>
  | IOption<"timeRangePicker", F, TimeRangePickerProps, FormItemProps>
  | IOption<"loadSelect", F, LoadSelectProps, FormItemProps, TSelectSlotKey>
  | IOption<"radio", F, RadioGroupProps, FormItemProps>
  | ICustomOption<F, object, FormItemProps>;
export type TFormOptionExtend = TExtends<FormOption<never>>;
